import 'package:dio/dio.dart';
import 'package:fsharing/core/models/product_detail.dart';
import 'package:fsharing/core/services/api.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

/// 商品详情
class ProductDetailModel extends ViewModel {
  final Api api;
  final int mid;
  final int referStoreId;

  String _errorMessage;
  ProductDetail _productDetail;

  ProductDetailModel(this.api, this.mid, this.referStoreId) : super() {
    _initDate();
  }

  String get errorMessage => _errorMessage;

  ProductDetail get productDetail => _productDetail;

  Future<ViewState> _initDate() async {
    viewState = ViewState.busy;
    try {
      _productDetail = await api.productDetail(null, mid, referStoreId, cancelToken);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
